package com.sinux.generality.provider.demo.tool;


import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

@Component
public class MyHttpUtil {
    public static String proxyIp;
    public static int proxyPort;
    public static String getWithAuth(String url, String tokenApi) {
        String result = null;
        BufferedReader br = null;
        InputStream input = null;
        try {
//            String url = "URL路径";
//            Map<String, String> prarms = new HashMap<>();
//            prarms.put("userName", "案例");
//            prarms.put("age", "18");
//            String jsonPrarms = JSON.toJSONString(params);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();   //获取浏览器信息
            HttpGet httpGet = new HttpGet(url);
            HttpHost proxy = new HttpHost(proxyIp, proxyPort);
            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
//        String encoding = DatatypeConverter.printBase64Binary("username:password".getBytes("UTF-8"));  //username  password 自行修改  中间":"不可少
            httpGet.setConfig(config);
            httpGet.setHeader("Authorization", "Bearer " + tokenApi);
            //httpPost.addHeader(new BasicHeader("Authorization","Basic " + Base64.encode("用户名:密码".getBytes())));
//            HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/json", "UTF-8"));  //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
//        httpGet   //把参数添加到post请求

            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();   //获取请求对象中的响应行对象
            int responseCode = statusLine.getStatusCode();
            if (responseCode == 200) {
                //获取响应信息
                HttpEntity entity = response.getEntity();
                String str1 = EntityUtils.toString(entity);
//                input = entity.getContent();
//                br = new BufferedReader(new InputStreamReader(input, "utf-8"));
//                String str1 = br.readLine();
                System.out.println("服务器的响应是:" + str1);
                return str1;
            } else {
                System.out.println("响应失败");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(br!=null){
                    br.close();
                }
                if(input!=null){
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return result;
    }

    public static String postWithAuth(String url, String jsonParams, String tokenApi) {
        String result = null;
        BufferedReader br = null;
        InputStream input = null;
        try {
//            String url = "URL路径";
//            Map<String, String> prarms = new HashMap<>();
//            prarms.put("userName", "案例");
//            prarms.put("age", "18");
//            String jsonPrarms = JSON.toJSONString(params);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();   //获取浏览器信息
            HttpPost httpPost = new HttpPost(url);
//            HttpHost proxy = new HttpHost(proxyIp, proxyPort);
//            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
//        String encoding = DatatypeConverter.printBase64Binary("username:password".getBytes("UTF-8"));  //username  password 自行修改  中间":"不可少
//            httpPost.setConfig(config);
            httpPost.setHeader("Authorization", "Bearer " + tokenApi);
            //httpPost.addHeader(new BasicHeader("Authorization","Basic " + Base64.encode("用户名:密码".getBytes())));
            HttpEntity entityParam = new StringEntity(jsonParams, ContentType.create("application/json", "UTF-8"));  //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
//        httpGet   //把参数添加到post请求
            httpPost.setEntity(entityParam);
            HttpResponse response = httpClient.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();   //获取请求对象中的响应行对象
            int responseCode = statusLine.getStatusCode();
            if (responseCode == 200) {
                //获取响应信息
                HttpEntity entity = response.getEntity();
                String str1 = EntityUtils.toString(entity);
//                input = entity.getContent();
//                br = new BufferedReader(new InputStreamReader(input, "utf-8"));
//                String str1 = br.readLine();
                System.out.println("服务器的响应是:" + str1);
                return str1;
            } else {
                System.out.println("响应失败");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(br!=null){
                    br.close();
                }
                if(input!=null){
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return result;
    }
}
